﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 25/04/2009
// Time: 19:01
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmBprDetail.
	/// </summary>
	public partial class frmBprDetail : KryptonForm
	{
		
		#region Declaration variables
			private general.Statuss listStatus = new csharp.rad.general.Statuss();
			public  Payment p = new Payment();
			private Payments listPayments = new Payments();
			public  BprDetail bprDetail = new BprDetail();
		#endregion
		
		public frmBprDetail()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		#region public functons
			private void fillComboStatus()
			{
				int tam = listStatus.count();
				
				cmbStatus.Items.Clear();
				cmbStatus.Text = " ";
				
				for(int i = 0; i < tam; i++)
				{
					cmbStatus.Items.Add(listStatus.get(i));
				}
				
				cmbStatus.ValueMember   = "id";
				cmbStatus.DisplayMember = "name";
			}
			
			private void clear()
			{
				txtParcela.Text 			= "";
				txtDocumento.Text			= "";
				txtHistorico.Text			= "";
				txtDocBancario.Text			= "";
				txtPorcJuros.Text			= "0";
				txtPorcMulta.Text			= "0";
				txtMultaApos.Text		    = "5";
				dtpEmissao.Value            = DateTime.Now;
				dtpPaymentDate.Value		= DateTime.Now;
				dtpPagData.Value			= DateTime.Now;
				dtpVecimento.Value			= DateTime.Now;
				txtPagValorPago.Text		= "0,00";
				txtValor.Text				= "0,00";
				txtJuros.Text				= "0,00";
				txtMulta.Text				= "0,00";
				txtDesconto.Text			= "0,00";
				txtTotal.Text				= "0,00";
				cmbStatus.SelectedIndex 	= -1;
				txtPagValor.Text			= "0,00";
				txtPagJuros.Text			= "0,00";
				txtPagMulta.Text			= "0,00";
				txtPagDesconto.Text			= "0,00";
				txtPagValorPago.Text		= "0,00";
				textPagObs.Text 			= "";
			}
			
			private void setClass()
			{
				/*Set class payment*/
				p.bpr_detail_id 			= bprDetail.id;
				p.user_id					= Information.userId;
				p.value 					= txtPagValorPago.Text;
				p.penalty_value				= txtPagMulta.Text;
				p.pay_day					= dtpPagData.Text;
				p.paid_value   				= txtPagValorPago.Text;
				p.obs						= textPagObs.Text;
				p.juros_value				= txtPagJuros.Text;
				p.discount_value			= txtPagDesconto.Text;
				p.date						= DateTime.Now.ToString();
				
				/*Set class bprDetails*/
				bprDetail.number 			= txtParcela.Text;
				bprDetail.documentNumber	= txtDocumento.Text;
				bprDetail.historic 			= txtHistorico.Text;
				bprDetail.bankNumber		= txtDocBancario.Text;
				bprDetail.percJuros			= txtPorcJuros.Text;
				bprDetail.percPenalty		= txtPorcMulta.Text;
				bprDetail.penaltyAfterDays  = txtMultaApos.Text;
				bprDetail.paymentDate		= dtpPaymentDate.Text;
				bprDetail.jurosValue		= txtJuros.Text;
				bprDetail.penaltyValue		= txtMulta.Text;
				bprDetail.discountValue		= txtDesconto.Text;
				bprDetail.paidValue			= txtTotal.Text;
			}
			
			private void setEdits()
			{
				p.clear();
				
				if(listPayments.searchByBprDetail(bprDetail.id))
				{
					//p.Payment_bpr_detail_id 	= bprDetail.id;
					//p.Payment_user_id			= Information.userId;
					txtPagValor.Text = listPayments.get(0).value;
					txtPagMulta.Text = listPayments.get(0).penalty_value;
					dtpPagData.Value    = Convert.ToDateTime(listPayments.get(0).pay_day);
					txtPagValorPago.Text = listPayments.get(0).paid_value;
					textPagObs.Text     = listPayments.get(0).obs;
					txtPagJuros.Text    = listPayments.get(0).juros_value;
					txtPagDesconto.Text = listPayments.get(0).discount_value;
					
					p = listPayments.get(0);
				}		
				
				if(bprDetail.get())
				{
					txtParcela.Text 			= bprDetail.number;
					txtDocumento.Text			= bprDetail.documentNumber;
					txtHistorico.Text			= bprDetail.historic;
					txtDocBancario.Text			= bprDetail.documentNumber;
					txtPorcJuros.Text			= bprDetail.percJuros;
					txtPorcMulta.Text			= bprDetail.percPenalty;
					txtMultaApos.Text			= bprDetail.penaltyAfterDays;
					dtpEmissao.Value            = Convert.ToDateTime(bprDetail.emissionDate);
					//dtpPaymentDate.Value		= Convert.ToDateTime(bprDetail.paymentDate);
					dtpVecimento.Value			= Convert.ToDateTime(bprDetail.expirationDate);
					txtPagValorPago.Text		= bprDetail.paidValue;
					txtValor.Text				= bprDetail.value;
					
					//Se não pagou, calcula os juros 
					if (!p.id.Equals(string.Empty))
					{
						double value 				= Convert.ToDouble(bprDetail.value);//Valor para gerar o Total
						double juros 				= Convert.ToDouble(bprDetail.jurosValue);//Valor para gerar o Total
						double penalty  			= Convert.ToDouble(bprDetail.penaltyValue);//Valor para gerar o Total
						double discount 			= Convert.ToDouble(bprDetail.discountValue);//Valor para gerar o Total						
						
						//Calcular os valores aqui
						
						
						
						
						txtTotal.Text				= Convert.ToString(value + (juros + penalty) - discount);//Total
						txtJuros.Text				= "";
						txtMulta.Text				= "";
						txtDesconto.Text			= "";
					
						txtPagValor.Text            = "";
						txtPagMulta.Text = "";
    					txtPagValorPago.Text = "";
						txtPagJuros.Text    = "";
						txtPagDesconto.Text = "";						
					}
					else
					{
						txtJuros.Text				= bprDetail.jurosValue;
						txtMulta.Text				= bprDetail.penaltyValue;
						txtDesconto.Text			= bprDetail.discountValue;					
						txtTotal.Text				= bprDetail.value;//Total
						txtPagValor.Text            = bprDetail.paidValue;						
					}
					
					for (int i=0; i < listStatus.count(); i++)
					{
						if (listStatus.get(i).id.Equals(bprDetail.statusId))
						{
							cmbStatus.SelectedIndex = i;
							break;
						}
					}					
				}
			}
			
			private void save()
			{
				if(cmbStatus.SelectedIndex > 0)
				{
					bprDetail.statusId = (cmbStatus.SelectedItem as general.Status).id;
				}
				else
				{
					KryptonMessageBox.Show("Selecione o status");
					return;	
				}
				
				setClass();				
				if(!bprDetail.update())
				{
					KryptonMessageBox.Show(bprDetail.errorMessage);
					return;
				}
				

				if(p.id == String.Empty)
				{
					if(!p.save())
					{
						KryptonMessageBox.Show(p.errorMessage);
						return;
					}
				}
				else
				{
					if(!p.update())
					{
						KryptonMessageBox.Show(p.errorMessage);
						return;
					}
				}
				KryptonMessageBox.Show("Registro salvo com sucesso");
			}
		#endregion
		
		void FrmBprDetailLoad(object sender, EventArgs e)
		{
			listStatus.fillList("bpr");
			fillComboStatus();
			
			setEdits();
			
			txtPagValor.Focus();
		}
		
		
		void KryptonButton1Click(object sender, EventArgs e)
		{
			save();
		}
		
		void BtnExitClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
